var app = {};
app.site_url = 'http://localhost:1234/series/project_Try/';
  $(function() {

    $("#shiftDate").datepicker();
    $('#startTime').datetimepicker();
    $('#endTime').datetimepicker();

    app.shifts = $('#boxAddShift');
    app.list = $('#boxShifts');
    app.error = $('#divError');

    app.error.hide();
    app.shifts.hide();
    app.list.show();

    var companyId = 0;

    $('.btn-primary').click(function(){
        app.error.hide();
        app.shifts.show();
        app.list.hide();
        $('#shiftId').val("");
        $('#shiftDate').val("");
        $('#startTime').val("");
        $('#endTime').val("");

        if($(this).attr('data-id'))
        {
          companyId = $(this).attr('data-id');
          var url = app.site_url+'getshift.php?id='+companyId;
          $.get(url, function(data){
              $('#shiftId').val(data.id);
              $('#shiftDate').val(data.shiftDate);
              $('#startTime').val(data.startTime);
              $('#endTime').val(data.endTime);
              $('#cmbRole').val(data.roleID);
              $('#cmbEmployee').val(data.empID);
              console.log(data);
          },'json');

        }
    });

    $('.btn-danger').click(function(){

        if($(this).attr('data-id') == 'delete')
        {
            var url = app.site_url + 'delete.php?id='+companyId;
            console.log(url);
            $.get(url, function(data){
               window.location = "";
            });
        }
        else if($(this).attr('data-id'))
        {
          companyId = $(this).attr('data-id');
          $('#myModal').modal();
        }
    });

    $('#btnCancel').click(function(){
      app.error.hide();
      app.shifts.hide();
      app.list.show();
    });


    $('.btn-success').click(function(){
        app.error.hide();
        if($('#shiftDate').val() === "") {
            app.error.html('<a class="close" data-dismiss="alert" href="#">x</a>Shift Date is Required!')
                     .fadeIn();
          return;
        }
        if($('#startTime').val() === "") {
            app.error.html('<a class="close" data-dismiss="alert" href="#">x</a>Shift Start time is Required!')
                     .fadeIn();
          return;
        }
        if($('#endTime').val() === "") {
            app.error.html('<a class="close" data-dismiss="alert" href="#">x</a>Shift End Time is Required!')
                     .fadeIn();
          return;
        }

        var postData;
        if($('#shiftId').val() === "")
        {
          postData = {
            roleID: $('#cmbRole').val(),
            empID: $('#cmbEmployee').val(),
            shiftDate: $('#shiftDate').val(),
            startTime: $('#startTime').val(),
            endTime: $('#endTime').val()
          };
        }
        else
        {
          postData = {
            shiftId: $('#shiftId').val(),
            roleID: $('#cmbRole').val(),
            empID: $('#cmbEmployee').val(),
            shiftDate: $('#shiftDate').val(),
            startTime: $('#startTime').val(),
            endTime: $('#endTime').val()
          };
        }

        if($('#shiftId').val() === "")
        {
          $.post(app.site_url+"add.php", postData, function(data){
            if(data.result == "success")
            {
              window.location = "";
            }
            else
            {
              app.error.html('<a class="close" data-dismiss="alert" href="#">x</a>Error!')
                       .fadeIn();
            }
          },'json');
        }
        else
        {
          $.post(app.site_url+"modifyShift.php", postData, function(data){
              if(data.result == "success")
              {
                window.location = "";
              }
              else
              {
                app.error.html('<a class="close" data-dismiss="alert" href="#">x</a>Error!')
                         .fadeIn();
              }
           },'json');
        }
      });
  });
